package com.example.zhu.controller;

import com.example.zhu.dto.Student;
import com.example.zhu.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@RestController
@RequestMapping("/common")
@Slf4j
public class imgController {
    @Value("${img.path}")
    private String basePath;
    @Autowired
    private StudentService studentservice;

    @GetMapping("/download")
    public void download(String id, HttpServletResponse response) throws IOException {
            String fileImg = basePath + "/" +id+".jpg";
            FileInputStream fileInputStream = new FileInputStream(fileImg);
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("image/jpeg");
            int len = 0;
            //注意别写成大写
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
                outputStream.flush();
            }
            //关闭资源
            outputStream.close();
            fileInputStream.close();
    }
}